package com.lebang.schedule;

import com.alibaba.fastjson.JSONObject;
import com.lebang.component.RedisClient;
import com.lebang.feign.AppDictFeign;
import com.lebang.mapper.AdRecordLogMapper;
import com.lebang.model.AppDictModel;
import com.lebang.service.AndroidSdkErrorService;
import com.lebang.service.MemberSdkErrorService;
import com.lebang.service.cences.CencesAdRecordLogService;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @Author: hwq
 * @Date: 2020/12/18 10:49
 * @Version 1.0
 */
@Slf4j
@Component
public class AdLogTask {
    @Autowired
    private AdRecordLogMapper adRecordLogMapper;

    @Autowired
    private MemberSdkErrorService memberSdkErrorService;

    @Autowired
    private AndroidSdkErrorService androidSdkErrorService;
    @Autowired
    private CencesAdRecordLogService cencesAdRecordLogService;
    @Autowired
    private AppDictFeign feign;
    @Value("${info.appId}")
    private Integer appId;
    @Autowired
    private RedisClient redisClient;

    /**
     * 每日5点删除一个月前的旧数据
     */
    @Scheduled(cron = "0 0 5 * * ?")
    public void execute() {
        LocalDateTime starTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now().minusMonths(1), LocalTime.MAX);


    }

    /**
     * 统计用户数据源错误码
     */
    @Scheduled(cron = "${task.memberSdkError}")
    public void memberSdkErrorExecute() {
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        memberSdkErrorService.starDataInit(endTime);

    }


    /**
     * 统计手机型号数据源错误码
     */
    @Scheduled(cron = "${task.androidSdkError}")
    public void androidSdkErrorExecute() {
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        androidSdkErrorService.starDataInit(endTime);

    }

    /**
     * 统计广告填充
     */
    @Scheduled(cron = "${task.adRecord}")
    public void adError() {
        if (redisClient.tryGetDistributedLock("lock:Scheduled:censusAdRecord", UUIDUtil.M(), 100000)) {
            Integer value=null;
            try {
                JsonData jsonData = feign.get("ad_max_count", appId);
                if(jsonData.isSuccess()){
                    AppDictModel appDictModel = JSONObject.parseObject(JSONObject.toJSONString(jsonData.getData()), AppDictModel.class);
                    value=Integer.parseInt(appDictModel.getKeyvalue());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
            //广告填充统计
            cencesAdRecordLogService.starDataInit(endTime,value);
        }else {
            //其他系统执行
            log.info("其他系统执行");
        }
    }




}
